﻿using PlayHooky;
using Syncfusion.Licensing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 天机万年历
{
    public class HookAndCrack
    {
        public static void HC()
        {

            HookManager manager = new HookManager();
            manager.Hook(typeof(PublicKeyDecrypt).GetMethod("SyncfusionDecode"), typeof(HookAndCrack).GetMethod("SyncfusionDecodeHook"));

            string y = Syncfusion.CoreAssembly.StudioCoreVersion;
            y = "@" + SyncfusionCrack.HexEncode(SyncfusionCrack.String2ByteArray(y)) + "==";
            Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(Convert.ToBase64String(Encoding.UTF8.GetBytes(y)));
            //Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("NDg0MzdAMzEzNzJlMzIyZTMwb052b1AyaXZiVWJQaHBvcGhOQlMzNDF3N3RUOXFwdElCVVFremhpeHpzMD0=");
        }
        private static BitVector64.Section mySect1;

        private static BitVector64.Section mySect2;

        private static BitVector64.Section mySect3;

        private static BitVector64.Section mySect4;

        private static BitVector64.Section mySect5;
        public static void InitializeBitVector(int version)
        {
            mySect1 = BitVector64.CreateSection(short.MaxValue);
            mySect2 = BitVector64.CreateSection(2, mySect1);
            if (version > 15)
            {
                mySect3 = BitVector64.CreateSection(21, mySect2);
                mySect4 = BitVector64.CreateSection(21, mySect3);
            }
            mySect5 = BitVector64.CreateSection(14, mySect4);
        }
        public static byte[] SyncfusionDecodeHook(string key)
        {
            var v = Syncfusion.CoreAssembly.StudioCoreVersion;
            var l = v.Split('.');
            var version = int.Parse(l[0]);
            var version1 = ulong.Parse(l[1]);
            var version2 = ulong.Parse(l[2]);
            InitializeBitVector(version);
            var bv = new BitVector64((long)0);
            bv[mySect1] = 36500;
            bv[mySect3] = (ulong)version;
            bv[mySect4] = (ulong)version1;
            bv[mySect5] = (ulong)version2;
            var st = BitConverter.GetBytes(bv.Data);
            Array.Copy(new byte[] { 0x61, 0x89, 0x00, 0x00 }, 0, st, 4, 4);
            byte[] ed = { 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0 };


            byte[] tmp = new byte[st.Length + ed.Length];
            Array.Copy(st, 0, tmp, 0, st.Length);
            Array.Copy(ed, 0, tmp, st.Length, ed.Length);
            return tmp;
        }
    }
}
